<template>
  <div>
    <!-- 添加博客分类组件 -->
    <div style="margin-top: 15px">
      <el-input
        placeholder="请输入分类名称，左边下拉列表选择分类等级"
        v-model="input"
        class="input-with-select addinput-container"
      >
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="5" value="5"></el-option>
        </el-select>
      </el-input>
      <el-button
        style="margin-left: 10px"
        type="primary"
        @click="addBlogTypeHandle"
        >添加</el-button
      >
    </div>

    <!-- 数据表 -->
    <el-table :data="data" style="width: 100%" border>
      <el-table-column prop="index" label="序号" width="150" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="title" label="博客类别" align="center">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>

      <el-table-column prop="description" label="文章数量" align="center">
        <template slot-scope="scope">{{ scope.row.articleCount }}</template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editBlogTypeHandle(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteBlogTypeHandle(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑框 -->
    <el-dialog title="请编辑该分类信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序等级">
          <el-select v-model="form.order" placeholder="请选择博客分类等级">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
            <el-option label="5" value="5"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBlogTypeConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addBlogType,
  getBlogType,
  deleteBlogType,
  updateBlogType,
  getOneBlogType,
} from "@/api/blogType";
export default {
  data() {
    return {
      input: "",
      select: "1",
      data: [],
      form: { name: "", order: "", id: "" },
      dialogFormVisible: false,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getBlogType().then((res) => {
        this.data = res.data;
      });
    },
    //添加博客类别
    addBlogTypeHandle() {
      //判断输入内容是否为空
      if (this.input.trim() !== "") {
        addBlogType({ name: this.input, order: this.select }).then((res) => {
          this.$message.success("添加分类信息成功");
          this.fetchData();
          //恢复默认状态
          this.input = "";
          this.select = "1";
        });
      } else {
        this.$message.error("分类信息不能为空");
        this.input = "";
      }
    },
    //删除博客分类
    deleteBlogTypeHandle({ id }) {
      //弹出提示框询问是否删除
      this.$confirm(
        "此操作将永久删除该博客分类, 是否继续?",
        "是否删除此博客分类",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          //发送删除请求
          deleteBlogType(id).then(() => {
            //重新获取所有文章数据
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //打开博客分类编辑框
    editBlogTypeHandle({ id }) {
      //拿到该博客分类
      getOneBlogType(id).then((res) => {
        this.form = res.data;
      });
      this.dialogFormVisible = true;
    },
    //更新博客分类信息
    editBlogTypeConfirm() {
      updateBlogType(this.form.id, {
        name: this.form.name,
        order: this.form.order,
      }).then((res) => {
        this.$message.success("博客分类信息更新成功");
        this.fetchData();
        this.dialogFormVisible = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.addinput-container {
  width: 400px;
  margin-bottom: 30px;
}
</style>
